<?php

require_once dirname(__FILE__).'/../lib/contractGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/contractGeneratorHelper.class.php';

/**
 * contract actions.
 *
 * @package    sf_sandbox
 * @subpackage contract
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class contractActions extends autoContractActions
{
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $requestparams=$request->getParameter($form->getName());
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    
    $error="";
    
    if($requestparams["bid_amount"]<=0)
    {
      $error="Invalid amount: ".$requestparams["bid_amount"];
    }
    
    if($error)
    {
      $this->getUser()->setFlash('error', $error);
      $this->redirect($this->request->getReferer());
    }
    elseif ($form->isValid())
    {
      $this->getUser()->setFlash('notice', $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.');

      $contract = $form->save();
      $project=$contract->getProject();
      $project->calc();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $contract)));

      $this->redirect('@project_show?id='.$contract['project_id']);
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.');
      $this->redirect($this->request->getReferer());
    }
  }
  public function executeDelete(sfWebRequest $request)
  {
    //$request->checkCSRFProtection();
    if($request->getParameter("contract[_csrf_token]")=="")die();

    //$this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));

    $this->forward404Unless($contract = Doctrine::getTable('Contract')->find(array($request->getParameter('contract[id]'))), sprintf('Object contract does not exist (%s).', array($request->getParameter('id'))));


//    $contract=$this->getRoute()->getObject();
    $project=$contract->getProject();

    $contract->delete();
    $project->calc();

    $this->getUser()->setFlash('notice', 'The item was deleted successfully.');

    $this->redirect($this->request->getReferer());
  }
}
